﻿@page "/user_auth/role"
@using Gardener.Application.Dtos
<div>
    <PageContainer Title="">
        <Breadcrumb>
            <Breadcrumb>
                <BreadcrumbItem>用户权限</BreadcrumbItem>
                <BreadcrumbItem>角色管理</BreadcrumbItem>
            </Breadcrumb>
        </Breadcrumb>
        <ChildContent>
            <Table @ref="table"
                   TItem="RoleDto"
                   DataSource="@roles"
                   Total="_total"
                   OnChange="@onChange"
                   @bind-PageIndex="_pageIndex"
                   @bind-PageSize="_pageSize"
                   @bind-SelectedRows="selectedRows"
                   Loading="@tableIsLoading"
                   Context="model">
                <TitleTemplate>
                    <Row>
                        <AntDesign.Col Span="8">
                            <ResourceAuthorize ResourceKey="user_auth_role_delete_selected">
                                <Button Type="@ButtonType.Primary" Icon="delete" Danger OnClick="OnDeletesClick">
                                    删除选中
                                </Button>
                            </ResourceAuthorize>
                        </AntDesign.Col>
                        <AntDesign.Col Span="4" Offset="12" Style="text-align:right">
                            <Space>
                                <ResourceAuthorize ResourceKey="user_auth_role_add">
                                    <SpaceItem>
                                        <Button Type="@ButtonType.Primary" Icon="plus" OnClick="OnAddClick">
                                            添加
                                        </Button>
                                    </SpaceItem>
                                </ResourceAuthorize>
                                <ResourceAuthorize ResourceKey="user_auth_role_refresh">
                                    <SpaceItem>
                                        <Button Type="@ButtonType.Primary" Icon="reload" OnClick="OnReLoadTable">
                                            刷新
                                        </Button>
                                    </SpaceItem>
                                </ResourceAuthorize>
                            </Space>
                        </AntDesign.Col>

                    </Row>

                </TitleTemplate>
                <RowTemplate>
                    <ResourceAuthorize ResourceKey="user_auth_role_delete_selected">
                        <Selection Key="@(model.Id.ToString())" />
                    </ResourceAuthorize>
                    <Column Title="编号" @bind-Field="@model.Id" Sortable />
                    <Column Title="名称" @bind-Field="@model.Name" />
                    <Column Title="备注" @bind-Field="@model.Remark" />
                    <Column Title="是否时超级管理" Field="@model.IsSuperAdministrator">
                        <Tag Color="@(model.IsSuperAdministrator?"blue":"red")">
                            @(model.IsSuperAdministrator?"是":"否")
                        </Tag>
                    </Column>
                    <Column Title="是否是默认角色" Field="@model.IsDefault">
                        <Tag Color="@(model.IsDefault?"blue":"red")">
                            @(model.IsDefault ? "是":"否")
                        </Tag>
                    </Column>
                    <Column Title="是否锁定" Field="@model.IsLocked">
                        <ResourceAuthorize ResourceKey="user_auth_role_lock">
                            <Authorized>
                                <Switch @bind-Value="@model.IsLocked" OnChange="e=>OnChangeIsLocked(model,e)"></Switch>
                            </Authorized>
                            <NotAuthorized>
                                <Tag Color="@(!model.IsLocked?"blue":"red")">
                                    @(model.IsLocked ? "是":"否")
                                </Tag>
                            </NotAuthorized>
                        </ResourceAuthorize>
                    </Column>
                    <Column Title="创建时间" @bind-Field="@model.CreatedTime" Format="@SystemConstant.DateTimeFormat" Sortable />
                    <ActionColumn>
                        <Space>
                            <ResourceAuthorize ResourceKey="user_auth_role_edit">
                                <SpaceItem><Button Type="@ButtonType.Primary" Size="@SystemConstant.OperationButtonSize" OnClick="()=>OnEditClick(model)">编辑</Button></SpaceItem>
                            </ResourceAuthorize>
                            <ResourceAuthorize ResourceKey="user_auth_role_set_resource">
                                <SpaceItem><Button Type="@ButtonType.Primary" Size="@SystemConstant.OperationButtonSize" OnClick="()=>OnEditRoleResourceClick(model)">分配资源</Button></SpaceItem>
                            </ResourceAuthorize>
                            <ResourceAuthorize ResourceKey="user_auth_role_delete">
                                <SpaceItem><Button Type="@ButtonType.Primary" Danger OnClick="()=>OnDeleteClick(model.Id)">删除</Button></SpaceItem>
                            </ResourceAuthorize>
                        </Space>
                    </ActionColumn>
                </RowTemplate>
            </Table>

        </ChildContent>
    </PageContainer>
    <Drawer Closable="true" Width="450" Visible="drawerVisible" Placement="right" Title="@drawerTitle" OnClose="_=>OnDrawerClose()">
        @if (editModel != null)
        {
            <Form Loading="@formIsLoading"
                  Model="@editModel"
                  LabelCol="new ColLayoutParam { Span = 8 }"
                  WrapperCol="new ColLayoutParam { Span = 16 }"
                  OnFinish="OnFormFinish"
                  OnFinishFailed="OnFormFinishFailed"
                  Context="model">
                <FormItem Label="名称">
                    <Input @bind-Value="@model.Name" />
                </FormItem>
                <FormItem Label="备注">
                    <TextArea AutoSize="true" MinRows="2" MaxRows="6" @bind-Value="@model.Remark"></TextArea>
                </FormItem>
                <FormItem Label="是否是超级管理员">
                    <Switch @bind-Value="@model.IsSuperAdministrator"></Switch>
                </FormItem>
                <FormItem Label="是否是默认角色">
                    <Switch @bind-Value="@model.IsDefault"></Switch>
                </FormItem>
                <FormItem Label="是否锁定">
                    <Switch @bind-Value="@model.IsLocked"></Switch>
                </FormItem>
                <FormItem WrapperCol="new ColLayoutParam { Offset = 4, Span = 20 }">
                    <Button Type="@ButtonType.Primary" HtmlType="submit">
                        保存
                    </Button>
                    <Button OnClick="_=>OnDrawerClose()">
                        取消
                    </Button>
                </FormItem>
            </Form>
        }
    </Drawer>


    <Drawer Closable="true" Width="450" Visible="editRoleResourceDrawerVisible" Placement="right" Title="@editRoleResourceDrawerTitle" OnClose="_=>OnEditRoleResourceDrawerClose()">
        <Row>
            <AntDesign.Col>
                <Space>
                    <SpaceItem>
                        <Tooltip Placement="PlacementType.TopLeft" Title="@(isExpanded?"收起":"展开")">
                            <Button Type="@ButtonType.Primary" Icon="@(isExpanded?"up":"down")" Size="@SystemConstant.OperationButtonSize" OnClick="OnExpandClick" />
                        </Tooltip>
                    </SpaceItem>
                    <SpaceItem><Button Type="@ButtonType.Primary" Size="@SystemConstant.OperationButtonSize" OnClick="OnEditRoleResourceSave">保存</Button></SpaceItem>
                    <SpaceItem><Button Type="@ButtonType.Default" Size="@SystemConstant.OperationButtonSize" OnClick="e=>OnEditRoleResourceDrawerClose()">取消</Button></SpaceItem>
                </Space>
            </AntDesign.Col>
        </Row>
        <Row>
            <AntDesign.Col Span="24">
                <Spin Spinning="@editRoleResourceTreeIsLoading">
                    <Tree @ref="tree"
                          ShowLine
                          ShowIcon
                          Checkable
                          Multiple
                          TitleExpression="x=> ((ResourceDto)x.DataItem).GetFullName()"
                          ChildrenExpression="x=>((ResourceDto)x.DataItem).Children"
                          IconExpression="x=>((ResourceDto)x.DataItem).Icon"
                          IsLeafExpression="x=>false"
                          Style="min-height:466px;min-width:400px">
                    </Tree>
                </Spin>
            </AntDesign.Col>
        </Row>

    </Drawer>
</div>